# -*- coding: utf-8 -*-
#
#	Copyright (C) 2011 by Igor E. Novikov
#	
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>. 

from PyQt4 import QtGui, QtCore

from fe import events

class AppAction(QtGui.QAction):

	mode = None

	def __init__(self, icon, text, obj, method, shortcut='',
				channels=[], validator=None):

		QtGui.QAction.__init__(self, icon, text, obj)

		if shortcut:
			self.setShortcut(shortcut)

		self.method = method
		obj.connect(self, QtCore.SIGNAL('triggered()'), method)

		self.channels = channels
		self.validator = validator

		if channels:
			for channel in channels:
				events.connect(channel, self.receiver)

	def receiver(self, *args):
		self.setEnabled(self.validator())


def create_actions(app):

	actions = {}

	actions['NEW'] = AppAction(app.generic_icons['NEW'],
							app.tr('New'),
							app, app.new,
							'Ctrl+N')
	actions['OPEN'] = AppAction(app.generic_icons['OPEN'],
							app.tr('Open...'),
							app, app.open,
							'Ctrl+O')
	actions['SAVE_AS'] = AppAction(app.generic_icons['SAVE_AS'],
							app.tr('Save As...'),
							app, app.save_as)

	actions['EXIT'] = AppAction(app.generic_icons['EXIT'],
							app.tr('Exit'),
							app, app.app_exit,
							'Ctrl+Q')

	return actions
